/**
 * Created by bai on 2015/7/17.
 */
var OpenTable = function(){
    var ORDERID  = "";//开台存储订单id
    /*
     * 改变表头的信息
     * */
    var changeHeaderTitle = function(name){
        $('.header .headerTitle').html(name);
    }
    /*
     * 获得cookie
     * */
    /*
     * 得到cookie
     * */
    function getCookie(c_name)
    {
        if (document.cookie.length>0)
        {
            var c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1)
            {
                var c_start=c_start + c_name.length+1
                var c_end=document.cookie.indexOf(";",c_start)
                if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return ""
    }
    /*
     * 蒙布显示
     * */

    /************空桌开台的流程开始*******************/
    /*
     * 点击空台
     * */
    var emptyTableOpen = function(obj,fun){
        var tableName=obj.data('tablename');
        var parentsNode=$('#subfoundingOrderPage');
        var tId = obj.data('id');
        OrderingSystem.getTableById(tId, function (data) {
            parentsNode.data('title',tableName);
            fun();
            //changeHeaderTitle(tableName);
            $('#subfoundingOrderPage').html('');
            $('<ul class="cont">\
               <!-- 第一列 图标和桌号-->\
           <li class="tableInfo">\
               <!-- 图标-->\
               <div class="tableIco"></div>\
               <!-- 桌号-->\
               <p class="tableNum">'+tableName+'</p>\
           </li>\
           <!-- 第二列 桌台状态-->\
           <li class="stateInfo">\
               <!-- 左边状态字样-->\
               <p class="stateFont">状态</p>\
               <!-- 右边的状态信息-->\
               <p class="state">空台</p>\
           </li>\
           <!-- 第三列 选择入座方式-->\
           <li class="choose">\
               <!-- 开台点餐-->\
               <div class="order" data-function="btn_orderingFood_clickCpOrdering" data-id="'+data.data[0].id+'"  data-number="'+data.data[0].tableCap+'">开台点餐</div>\
               <!-- 预定落台-->\
               <div class="destine" data-function="btn_orderingFood_reserveLuoZhuo" data-id="'+data.data[0].id+'" data-number="'+data.data[0].tableCap+'">预定落台</div>\
           </li>\
           </ul>').appendTo('#subfoundingOrderPage');
        });

    };
    /*
     * 空台 点击开台点餐按钮
     * */
    var emptyTableOpenClickCpOrder = function(obj,fun,cpListFun){
        //var pId = getCookie('bqcuser_PIN');
        var pId = localStorage.getItem('bqcuser_PIN');

        var tId = obj.data('id');
        var personCount = obj.data('number');
        //alert(obj.data('number'))
        var itemList = [];
        //OrderingSystem.setRequestAsync(false);
        OrderingSystem.createOrder(pId, tId, personCount, itemList, function (data) {
            cpListFun(fun,data.data[0].id,tId) ;
            ORDERID = data.data[0].id;
        });

    };
    /*
     * 空台  点击预定落台
     * */
    var emptyTableOpenClickReserveToTable = function(obj,fun){
        var tableId=obj.data('id');
        var options = {};
        options.successFunction = function (data) {
            $('#suborderAndSearchPage .orderList').html('');
            fun();
            $.each(data.data,function(k,v){
                var name=v.reserve.name
                var options = {};
                options.tableClassId = v.reserve.tableClassId;
                options.successFunction = function (data) {
                    if(name==""){
                        $('<li class="orderInfo active" data-function="btn_orderingFood_emptyTable_reserve_luoZhuo" data-tableid="'+tableId+'" data-orderid="'+v.id+'">\
                   电话：'+v.reserve.ResMobile+'&nbsp;用餐人数：'+data.data[0].cap+'人\
                    </li>').appendTo('#suborderAndSearchPage .orderList');
                    }else{

                        $('<li class="orderInfo active" data-function="btn_orderingFood_emptyTable_reserve_luoZhuo" data-tableid="'+tableId+'" data-orderid="'+v.id+'">\
              '+name+'，电话：'+v.reserve.ResMobile+'&nbsp;用餐人数：'+data.data[0].cap+'人\
              </li>').appendTo('#suborderAndSearchPage .orderList');
                    }
                };
                OrderingSystem.getTableClassByTableClassId(options);

            })
        };
        OrderingSystem.getReserveAcceptedOrderList(options);

    };
    /*
     * 空台，点击预定订单落台
     * */
    var emptyTableClickReserveOrderLuoZhuo = function(obj,fun){
        $('#suborderAndSearchPage .orderInfo').addClass('disabled');
        var options = {};
        options.orderId = obj.data('orderid');
        options.tableId = obj.data('tableid');
        options.successFunction = function (data) {

            OrderingSystem.orderLineItemPrinter( options.orderId, function (data) {

            })
            fun()
        };
        OrderingSystem.pushReserveOrderToTable(options);

    }
    /************空桌开台的流程开始*******************/
    /************预定开台的流程开始*******************/
    /*
     * 根据时间判断今日，明天，后天
     * */
    var getDate=function(date){

        var dataOfPort=date;
        var timeNew=new Date();
        var timeNewArray=[]
        var timeArra=['今日','明天','后天'];
        var timeArray=[];
        timeArray[0]=timeNew.getFullYear();
        timeArray[1]=(timeNew.getMonth()+1);
        timeArray[2]=timeNew.getDate()
        timeNew=timeArray.join("");
        var timeS=dataOfPort.split('-').join("");

        if(timeArra[(timeS-timeNew)]){
            return timeArra[(timeS-timeNew)];
        }else{
            return "过期"
        }

    }
    /*
     * 点击预定桌台
     * */
    var reserveTableOpen = function(obj,fun){
        var parentsNode = $('#subtureTakeOneSeatPage');
        var  request="";
        var notDishes = "";
        var hasDishes = "";
        var oId=obj.data('orderid');
        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";
            var request="";
            var time= getDate(data.data[0].reserve.ResDate)
            if(dataOfPort.request.length==0){

            }else{
                request= dataOfPort.request.specialRequest;
            }
            $.each(data.data[0].itemList,function(k,v){
                cpList+='<p class="dish">'+v.name+'（'+v.count+'）</p>'

            })
            if(data.data[0].itemList.length>0){
                notDishes = "none";
                hasDishes = "block";
            }else{
                notDishes = "block";
                hasDishes = "none";
            }
            $('<div class="takeOneSeat active" data-function="btn_orderingFood_reserve_LuoZuoOk" data-id="'+oId+'">\
                <div class="ico" data-function="btn_orderingFood_reserve_LuoZuoOk" data-id="'+oId+'"></div>\
                    <p class="true" data-function="btn_orderingFood_reserve_LuoZuoOk" data-id="'+oId+'">确认落座</p>\
                </div><div class="clearOrder active" data-function="btn_orderingFood_reserve_clearAwayReserve" data-id="'+oId+'">\
                <div class="ico" data-function="btn_orderingFood_reserve_clearAwayReserve" data-id="'+oId+'"></div>\
                <p class="true" data-function="btn_orderingFood_reserve_clearAwayReserve" data-id="'+oId+'">清除预定</p>\
                </div>').appendTo('#subtureTakeOneSeatPage .bottom');
            var options = {};
            options.tableClassId = dataOfPort.reserve.tableClassId;
            options.successFunction = function (dataT) {

                fun();
                $(".cont",parentsNode).html('');
                $('  <li class="stateInfo">\
                    <p class="state">状态</p>\
                    <p class="yeah">已预定</p>\
                    </li>\
                    <li class="personInfo">\
                        <p class="contactWay">联系方式</p>\
                        <p class="name">姓名：'+dataOfPort.reserve.name+'</p>\
                        <p class="phone">电话：<span class="phoneNum">'+dataOfPort.reserve.ResMobile+'</span></p>\
                        <a class="phoneIco" href="tel:'+dataOfPort.reserve.ResMobile+'"></a>\
                    </li>\
                    <li class="serviceInfo">\
                    <p class="serviceCont">服务内容</p>\
                    <p class="mealTime">用餐时间：'+time+dataOfPort.reserve.ResTime+'</p>\
                <p class="mealCount">用餐人数：'+dataT.data[0].cap+'人</p>\
                </li>\
                <div class="noOrderAndNoPay" style="display: '+notDishes+'">\
                    <li class="billInfo" data-function="todayOrder_reserve_chooseCp" data-id="'+dataOfPort.id+'">\
                        <p class="bill" data-function="todayOrder_reserve_chooseCp" data-id="'+dataOfPort.id+'">账单</p>\
                        <div class="nop" data-function="todayOrder_reserve_chooseCp" data-id="'+dataOfPort.id+'">\
                            <p class="noOrder" data-function="todayOrder_reserve_chooseCp" data-id="'+dataOfPort.id+'">未点餐</p>\
                            <p class="goToOrder" data-function="todayOrder_reserve_chooseCp" data-id="'+dataOfPort.id+'">〉</p>\
                        </div>\
                    </li>\
                </div>\
                <div class="orderAndPay" style="display: '+hasDishes+'">\
                    <div class="billInfo">\
                        <p class="bill">账单</p>\
                        <div class="yeah">'+cpList+'\
                        </div>\
                    </div>\
                    <li class="sumInfo">\
                    <p class="sum">合计</p>\
                    <p class="money">￥'+dataOfPort.total/100+'元</p>\
                    </li>\
                    </div>\
                    <li class="payInfo">\
                    <p class="payState">支付状态</p>\
                    <p class="yeahOrNo">未支付</p>\
                    </li>').appendTo('#subtureTakeOneSeatPage .cont');


            };
            OrderingSystem.getTableClassByTableClassId(options);


        });

    };
    /*
     * 今日订单选菜
     * */
    var todayOrderChooseCp = function(obj,fun){
        fun();
    };
    /*
     * 确认落座
     * */
    var reserveLuoZuoOk = function(obj,fun){
        /*   $('#subtureTakeOneSeatPage .IndexPage').show();
         $('#subtureTakeOneSeatPage .goToOrderOrNo').show();*/
        var orderId = obj.data('id');
        OrderingSystem.getTableList(function (data) {
            fun();
            $('.chooseSeatIndexPage').hide();
            $('.chooseSeatIndexMain').hide();
            var capital = ['','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾']
            $('#suborderingFoodchooseSeatPage .ul-table-list').html('');
            $('<div class="listTitle">\
                <h2>餐台列表</h2>\
                </div>').appendTo('#suborderingFoodchooseSeatPage .ul-table-list')
            $.each(data.data,function(k,v){
                if(v.tableStatus=="OPEN"){
                    var number=(v.tableCap<11)?capital[v.tableCap]:"拾";
                    $('<li data-function="btn_orderingFood_reserveList_tableList" data-id="'+v.id+'" data-orderid="'+orderId+'">\
                   <div data-function="btn_orderingFood_reserveList_tableList" data-id="'+v.id+'" data-orderid="'+orderId+'">'+number+'</div>\
                   <p data-function="btn_orderingFood_reserveList_tableList" data-id="'+v.id+'" data-orderid="'+orderId+'">'+v.label+'（空） '+v.tableClassName+'</p>\
                   </li>').appendTo('#suborderingFoodchooseSeatPage .ul-table-list');
                }

            })
        });


    };
    /*
     * 点击今日订单的落桌按钮
     * */
    var todayOrderReserveLuoZhuo = function(obj,fun){
        var options = {};
        options.orderId = obj.data('orderid');
        options.tableId = obj.data('id');
        options.successFunction = function (data) {
            fun();
        };
        OrderingSystem.pushReserveOrderToTable(options);

    }
    /*
     * 预定不点菜
     * */
    var reserveNoChooseCp = function(fun){
        var parentsNode = $('#subtureTakeOneSeatPage');
        $('.goToOrderOrNo',parentsNode).hide();
        $('.IndexPage',parentsNode).hide();
        fun();
    };
    /*
     * 预定点菜
     * */
    var reserveChooseCp = function(obj,fun){
        var parentsNode = $('#subtureTakeOneSeatPage');
        $('.goToOrderOrNo',parentsNode).hide();
        $('.IndexPage',parentsNode).hide();
        fun();
    };
    /*
     * 预定换台
     * */
    var reserveChangeTable = function(obj,fun){
        fun();
    }
    /*
     * 清除预定
     * */
    var reserveClearOrder= function(obj,fun){
        var parentsNode = $('#subClearReserveOrderTaskPage');
        var  request="";
        var notDishes = "";
        var hasDishes = "";
        var oId=obj.data('id');
        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";
            var request="";
            var time= getDate(data.data[0].reserve.ResDate)
            if(dataOfPort.request.length==0){

            }else{
                request= dataOfPort.request.specialRequest;
            }
            $.each(data.data[0].itemList,function(k,v){
                cpList+=v.name+'（'+v.count+'）'

            })
            if(data.data[0].itemList.length>0){
                notDishes = "none";
                hasDishes = "block";
            }else{
                notDishes = "block";
                hasDishes = "none";
            }
            $('.bottom',parentsNode).html('<div data-function="btn_orderingFood_clearReserveOrder" data-id="'+dataOfPort.id+'">确认清除</div>');
            var options = {};
            options.tableClassId = dataOfPort.reserve.tableClassId;
            options.successFunction = function (dataT) {

                fun();

                $(".cont",parentsNode).html('');
                $(' <div class="main">\
                    <dl class="menuChose">\
                    <div>预定方式</div>\
                    <p>微信预定</p>\
                    </dl>\
                        <dl class="personInfo">\
                            <div class="listHead">联系方式</div>\
                <pre>\
姓名：'+dataOfPort.reserve.name+'<br>\
电话：'+dataOfPort.reserve.ResMobile+'\
                </pre>\
                            <a></a>\
                        </dl>\
                        <dl class="severCont">\
                            <div>服务内容</div>\
                <pre>\
用餐时间：'+time+dataOfPort.reserve.ResTime+'<br>\
用餐人数：'+dataT.data[0].cap+'人\
                </pre>\
                        </dl>\
                        <dl class="menuChose">\
                            <div>账单情况</div>\
                            <p>'+cpList+'</p>\
                        </dl>\
                        <dl class="menuChose">\
                            <div>支付状态</div>\
                            <p>未支付</p>\
                            <h6> </h6>\
                        </dl>\
                        <dl class="refuseReason">\
                            <div>拒绝理由</div>\
                            <textarea cols="40" rows="4" class="todayOrder_refuse_reason"></textarea>\
                        </dl>\
                    </div>').appendTo('#subClearReserveOrderTaskPage .cont')



            };
            OrderingSystem.getTableClassByTableClassId(options);


        });

    }
    /*
     * 确认清除预定
     * */
    var reserveOrderClearOk = function(obj,fun){
        var parentsNode = $('#subClearReserveOrderTaskPage');
        if( $.trim($(".todayOrder_refuse_reason",parentsNode).val())) {
            var options = {};
            options.orderId = obj.data('id');
            options.reason = $(".todayOrder_refuse_reason",parentsNode).val();
            options.successFunction = function (data) {
                fun();
            };

            OrderingSystem.cancelReasonOrder(options);
        }else{
            alert("请输入订单理由")
        }



    }
    /************预定开台的流程结束*******************/
    /*
     *点击返回按钮，取消订单
     * */
    var clickBackCanlOrder = function(){
        if(ORDERID){
            var oId = ORDERID;
            OrderingSystem.cancelOrder(oId, function (data) {

            });
        }
    }
    return{
        //空台 点击开台点餐按钮
        emptyTableOpen:function(obj,fun){
            emptyTableOpen(obj,fun);
        },
        //点击预定桌台
        reserveTableOpen:function(obj,fun){
            reserveTableOpen(obj,fun)
        },
        //空台 点击开台点餐按钮
        emptyTableOpenClickCpOrder:function(obj,fun,cpListFun){
            emptyTableOpenClickCpOrder(obj,fun,cpListFun);
        },
        //空台  点击预定落台
        emptyTableOpenClickReserveToTable:function(obj,fun){
            emptyTableOpenClickReserveToTable(obj,fun);
        },
        //空台  点击预定订单
        emptyTableClickReserveOrderLuoZhuo:function(obj,fun){
            emptyTableClickReserveOrderLuoZhuo(obj,fun)
        },
        //确认落座
        reserveLuoZuoOk:function(obj,fun){
            reserveLuoZuoOk(obj,fun);
        },
        /*预定桌台不点菜*/
        reserveNoChooseCp:function(fun){
            reserveNoChooseCp(fun);
        },
        /*预定桌台点菜*/
        reserveChooseCp:function(obj,fun){
            reserveChooseCp(obj,fun);
        },
        /*预定换台*/
        reserveChangeTable:function(obj,fun){
            reserveChangeTable(obj,fun)
        },
        /*清除预定*/
        reserveClearOrder:function(obj,fun){
            reserveClearOrder(obj,fun);
        },
        /*确认清除预定*/
        reserveOrderClearOk:function(obj,fun){
            reserveOrderClearOk(obj,fun)
        },
        /*今日订单预定落桌*/todayOrderReserveLuoZhuo:function(obj,fun){
            todayOrderReserveLuoZhuo(obj,fun)
        },
        /*今日订单，预定订单选菜*/todayOrderChooseCp:function(obj,fun){
            todayOrderChooseCp(obj,fun);
        },
        /*返回按钮取消订单*/clickBackCanlOrder:function(){
            clickBackCanlOrder();
        }
    }
}();